package com.iptv.grape.ui.control.ad;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.view.ViewCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.iptv.grape.R;
import com.iptv.grape.domain.ChannelBill;
import com.iptv.grape.util.AppLog;

import java.util.List;
// 回看底部水平列表控件 适配器
public class LabelAdapter extends RecyclerView.Adapter<LabelAdapter.LabelHolder>  {

    private LayoutInflater mInflater;
    private Context context;
    private List<ChannelBill> channelBills;
    private ChannelBill selectChannelBill;


    public void setSelectChannelBill(ChannelBill selectChannelBill){
        this.selectChannelBill = selectChannelBill;
    }


    public static final int UPDATE_STATE = 101;
    public static final int UPDATE_NAME = 102;

    private int defItem = -1;

    public LabelAdapter(LayoutInflater inflater, Context context, List<ChannelBill> channelBills,ChannelBill selectChannelBill) {
        //this.list = list;
        //this.activity = activity;
        //inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.mInflater = inflater;
        this.context = context;
        this.channelBills = channelBills;
        this.selectChannelBill = selectChannelBill;
    }

    @NonNull
    @Override
    public LabelHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        AppLog.getCurLog().out("onCreateViewHolder调用");
        return new LabelHolder(mInflater.inflate(R.layout.bill_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull LabelHolder holder, int position) {
        //AppLog.getCurLog().out("onBindViewHolder2调用");
        final int pos = holder.getAdapterPosition();
        ChannelBill channelBill = this.channelBills.get(pos);

        setListener(holder,pos,channelBill);
        setUi(holder,channelBill);
    }
    @Override
    public void onBindViewHolder(@NonNull LabelHolder holder, int position, @NonNull List<Object> payloads) {
        //AppLog.getCurLog().out("onBindViewHolder1调用");
        if (payloads.isEmpty()) {
            onBindViewHolder(holder, position);
        }else if (payloads.get(0) instanceof Integer) {
            //AppLog.getCurLog().out("onBindViewHolder1调用");
//            int payLoad = (int) payloads.get(0);
//            switch (payLoad) {
//                case UPDATE_STATE:
//
//                    AppLog.getCurLog().out("UPDATE_STATE");
//                    holder.textView.setSelected(this.channelBills.get(position).getKeyID().equals(this.selectChannelBill.getKeyID()));
//                    break;
//                case UPDATE_NAME:
//                    holder.textView.setText(this.channelBills.get(position).getName());
//                    break;
//                default:
//                    break;
//            }
        }

    }

    private void setUi(LabelHolder holder,ChannelBill channelBill) {
        //AppLog.getCurLog().out("setUi" + channelBill.getName());
        holder.textView.setText(channelBill.getName());
        holder.time.setText(channelBill.getShowTimeText());
        switch (channelBill.getBillType()) {
            case "before":
                holder.linearLayout.setBackgroundResource(R.drawable.item_red_blus);
                break;
            case "future":
                holder.linearLayout.setBackgroundResource(R.drawable.item_gray_blus);
                break;
            case "now":
                holder.linearLayout.setBackgroundResource(R.drawable.item_yellow_blus);
                break;
        }
        if(selectChannelBill.getKeyID().equals(channelBill.getKeyID())){
            holder.relativeLayout.setSelected(true);
            holder.linearLayout.setBackgroundResource(R.drawable.item_gray_blus);
            //holder.relativeLayout.requestFocus();
            //selectViewHolder = holder;
        }
        else {
            holder.relativeLayout.setSelected(false);
        }

    }

    private void changeUi(boolean hasFocus, LabelHolder holder, ChannelBill channelBill) {
        //AppLog.getCurLog().out("changeUi调用");
        if(hasFocus){
            //this.lastViewHolder = holder;
            holder.linearLayout.setBackgroundResource(R.drawable.item_horitation_fucs);
            if(Build.VERSION.SDK_INT>=21){
                ViewCompat.animate(holder.linearLayout).scaleX(1.01f).scaleY(1.08f).translationZ(1).start();
            }else {
                ViewCompat.animate(holder.linearLayout).scaleX(1.01f).scaleY(1.08f).start();
                ViewGroup parent = (ViewGroup) holder.linearLayout.getParent();
                parent.requestLayout();
                parent.invalidate();

            }
        }else {
            switch (channelBill.getBillType()){
                case "before":
                    holder.linearLayout.setBackgroundResource(R.drawable.item_red_blus);
                    break;
                case "future":
                    holder.linearLayout.setBackgroundResource(R.drawable.item_gray_blus);
                    break;
                case "now":
                    holder.linearLayout.setBackgroundResource(R.drawable.item_yellow_blus);
                    break;
            }
            if(selectChannelBill.getKeyID().equals(channelBill.getKeyID())){
                holder.linearLayout.setBackgroundResource(R.drawable.item_gray_blus);
            }


//            holder.linearLayout.setBackgroundResource(R.drawable.item_horitation_blus);
            if(Build.VERSION.SDK_INT>=21){
                ViewCompat.animate(holder.linearLayout).scaleX(1.00f).scaleY(1.00f).translationZ(1).start();
            }else {
                ViewCompat.animate(holder.linearLayout).scaleX(1.00f).scaleY(1.00f).start();
                ViewGroup parent = (ViewGroup) holder.linearLayout.getParent();
                parent.requestLayout();
                parent.invalidate();

            }
        }
    }


    private OnItemFocusListener focusListener;
    private OnLabelClickListener onLabelClickListener;

    public interface OnItemFocusListener {
        void onItemFocus(ChannelBill channelBill,int position,boolean hasFocus);
    }
    public interface OnLabelClickListener {
        /**
         * 点击label
         *
         * @param channelBill     点击label的对象
         * @param position 点击位置
         */
        void onClick(ChannelBill channelBill, int position);
    }

    public void setOnItemFocusListener(OnItemFocusListener listener) {
        this.focusListener = listener;
    }
    public void setOnLabelClickListener(OnLabelClickListener onLabelClickListener) {
        this.onLabelClickListener = onLabelClickListener;
    }

    private void setListener(LabelHolder holder, int position, ChannelBill channelBill){
        InnClickListener inn = new InnClickListener(holder,position,channelBill);
        holder.relativeLayout.setOnClickListener(inn);
        holder.relativeLayout.setOnFocusChangeListener(inn);
        holder.relativeLayout.setFocusable(true);
    }

    class InnClickListener implements View.OnClickListener,View.OnFocusChangeListener{
        private LabelHolder holder;
        private int position;
        private ChannelBill channelBill;
        public InnClickListener(LabelHolder holder, int position, ChannelBill channelBill){
            this.holder = holder;
            this.position = position;
            this.channelBill = channelBill;
        }
        @Override
        public void onClick(View view) {
            if (onLabelClickListener != null) {
                onLabelClickListener.onClick(channelBill, position);
            }
        }

        @Override
        public void onFocusChange(View view, boolean hasFocus) {
            if(focusListener!=null){
                focusListener.onItemFocus(channelBill,position,hasFocus);
            }
            changeUi(hasFocus,holder,channelBill);
        }
    }

    @Override
    public int getItemCount() {
        //AppLog.getCurLog().out(this.channelBills.size() + "-------------------------------------------");
        return this.channelBills.size();
    }

    public class LabelHolder extends RecyclerView.ViewHolder {

        private TextView textView;
        private TextView time;
        private RelativeLayout relativeLayout;
        private LinearLayout linearLayout;
        private LinearLayout linearLayoutChange;

        public LabelHolder(@NonNull View itemView) {
            super(itemView);
            // name
            textView = itemView.findViewById(R.id.name);
            // time
            time = itemView.findViewById(R.id.time);
            // rel_isfucs
            relativeLayout=itemView.findViewById(R.id.rel_isfucs);
            // lin_background
            linearLayout=itemView.findViewById(R.id.lin_background);
            // lin_change
            linearLayoutChange=itemView.findViewById(R.id.lin_change);
        }


        public TextView getTextView() {
            return textView;
        }

        public TextView getTime() {
            return time;
        }

        public RelativeLayout getRelativeLayout() {
            return relativeLayout;
        }

        public LinearLayout getLinearLayout() {
            return linearLayout;
        }

        public LinearLayout getLinearLayoutChange() {
            return linearLayoutChange;
        }
    }
}
